/**
 *	@file	np_key.c
 *	@brief	木の実プランターアプリ 入力取得
 *	@author	Miyuki Iwasawa
 *	@date	08.08.04
 *
 */

#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "gflib/touchpanel.h"
#include "itemtool/item.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_n_planter.h"

#include "savedata/config.h"
#include "application/nut_planter.h"
#include "np_sys.h"
#include "np_tool.h"
#include "np_app.h"
#include "np_sub.h"
#include "np_time.h"
#include "np_key.h"
#include "np_snd_def.h"

#include "nplanter_gra.naix"
#include "data/clact_util/clact_header.naix"

//////////////////////////////////////////////////////////////////////////////
enum{
 EXE_BEDSEL_S01,
 EXE_BEDSEL_S02,
 EXE_BEDSEL_S03,
 EXE_BEDSEL_S04,
 EXE_BEDSEL_DECIDE,
 EXE_BEDSEL_BACK,
};

//////////////////////////////////////////////////////////////////////////////
//プロトタイプ
int exe_BedSelect(NP_APPWORK* wk,u32 code);
int key_BedSelect(NP_APPWORK* wk);
int touch_BedSelect(NP_APPWORK* wk,BOOL* exe_f);
int key_WaterSelect(NP_APPWORK* wk);
int touch_WaterSelect(NP_APPWORK* wk,BOOL* exe_f);

int touchsub_BedHitCheck(NP_APPWORK* wk);
int touchsub_BedSelectHitCheck(NP_APPWORK* wk);
int exesub_BedSelectDecide(NP_APPWORK* wk);




//////////////////////////////////////////////////////////////////////////////
/**
 *	@brief	ベッド選択入力取得
 */
int NPKey_InputBedSelect(NP_APPWORK* wk)
{
	int ret;
	BOOL exe_f = FALSE;

	ret = touch_BedSelect(wk,&exe_f);

	if(exe_f){
		return ret;
	}

	return key_BedSelect(wk);
}

/**
 *	@brief	連続水遣り選択入力取得
 */
int NPKey_InputWaterSelect(NP_APPWORK* wk)
{
	int ret;
	BOOL exe_f = FALSE;

	ret = touch_WaterSelect(wk,&exe_f);

	if(exe_f){
		return ret;
	}

	return key_WaterSelect(wk);
}

/*
 *	@brief	ベッド選択入力　実行処理
 */
int exe_BedSelect(NP_APPWORK* wk,u32 code)
{
	switch(code){
	case EXE_BEDSEL_S01:
	case EXE_BEDSEL_S02:
	case EXE_BEDSEL_S03:
	case EXE_BEDSEL_S04:
		Snd_SePlay( SND_NP_SELECT );
		NPSub_CursorPosSet(wk);
		break;
	case EXE_BEDSEL_DECIDE:
		if(wk->eff_tcb_ct != 0){
			break;
		}
		return exesub_BedSelectDecide( wk );
	case EXE_BEDSEL_BACK:
	default:
		if(wk->eff_tcb_ct != 0){
			break;
		}
		Snd_SePlay( SND_NP_SELECT );
		NPSub_BackButtonWrite(wk,BACK_BTN_ANM);
		return SEQ_FADE_OUT;
	}
	return SEQ_BED_SELECT;
}

/**
 *	@brief	ベッド選択入力　キー
 */
int key_BedSelect(NP_APPWORK* wk)
{
	u8 up = 0;


	if(sys.trg & PAD_BUTTON_KTST_CHG){
		wk->key_mode = APP_KTST_KEY;
	}

	if(sys.trg & PAD_BUTTON_B){
		return exe_BedSelect(wk,EXE_BEDSEL_BACK);
	}
	if(sys.trg & PAD_BUTTON_A){
		return exe_BedSelect(wk,EXE_BEDSEL_DECIDE);
	}
	if(sys.trg & PAD_KEY_LEFT){
		wk->c_pos = (wk->c_pos + (SEEDBED_MAX-1)) % SEEDBED_MAX;
		up = 1;
	}else if(sys.trg & PAD_KEY_RIGHT){
		wk->c_pos = (wk->c_pos + 1) % SEEDBED_MAX;
		up = 1;
	}
	if(up){
		return exe_BedSelect(wk,EXE_BEDSEL_S01+wk->c_pos);
	}
	return SEQ_BED_SELECT;
}

/**
 *	@brief	ベッド選択入力　タッチ
 */
int touch_BedSelect(NP_APPWORK* wk,BOOL* exe_f)
{
	int	ret;

	ret = touchsub_BedSelectHitCheck( wk );
	if(	ret < 0 ){
		return SEQ_BED_SELECT;
	}
	*exe_f = TRUE;
	wk->key_mode = APP_KTST_TOUCH;
	return exe_BedSelect(wk,ret);
}

/**
 *	@brief	連続水遣り入力取得　キー
 */
int key_WaterSelect(NP_APPWORK* wk)
{
	u8 up = 0;

	if(wk->pot_npos != 0xFF){
		return FALSE;	//既に予約済み
	}
	if(sys.trg & PAD_BUTTON_KTST_CHG){
		wk->key_mode = APP_KTST_KEY;
	}

	if(sys.trg & PAD_KEY_LEFT){
		wk->c_pos = (wk->c_pos + (SEEDBED_MAX-1)) % SEEDBED_MAX;
		up = 1;
	}else if(sys.trg & PAD_KEY_RIGHT){
		wk->c_pos = (wk->c_pos + 1) % SEEDBED_MAX;
		up = 1;
	}
	if(up){
		wk->pot_npos = wk->c_pos;
		CLACT_SetAnmFlag(wk->pAct[ACT_CURSOR], FALSE);
		NPSub_CursorPosSet(wk);
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	連続水遣り選択入力　タッチ
 */
int touch_WaterSelect(NP_APPWORK* wk,BOOL* exe_f)
{
	int	ret;

	if(wk->pot_npos != 0xFF){
		return FALSE;	//既に予約済み
	}
	ret = touchsub_BedHitCheck(wk);

	if(	ret < 0 || wk->c_pos == ret){
		return FALSE;
	}
	*exe_f = TRUE;
	wk->key_mode = APP_KTST_TOUCH;
	wk->c_pos = ret;
	wk->pot_npos = wk->c_pos;

	CLACT_SetAnmFlag(wk->pAct[ACT_CURSOR], FALSE);
	NPSub_CursorPosSet(wk);
	return TRUE;
}

/**
 *	@brief	ベッド　タッチ矩形判定
 */
int touchsub_BedHitCheck(NP_APPWORK* wk)
{
	RECT_HIT_TBL hit;
	int i;

	for(i = 0;i < SEEDBED_MAX ; i++){
		hit.rect.left = TP_SEED_X + TP_SEED_OX*i;
		hit.rect.right = hit.rect.left + TP_SEED_W;

		if(wk->seed[i].stat < SEEDSTAT_MIKI ){
			hit.rect.top = TP_SEED_Y0;
			hit.rect.bottom = hit.rect.top + TP_SEED_H0;
		}else{
			hit.rect.top = TP_SEED_Y1;
			hit.rect.bottom = hit.rect.top + TP_SEED_H1;
		}
		if( GF_TP_SingleHitTrg( &hit )){
			return i;
		}
	}
	return -1;

}

/**
 *	@brief	ベッド選択入力　タッチ矩形判定
 */
int touchsub_BedSelectHitCheck(NP_APPWORK* wk)
{
	int ret;

	static const RECT_HIT_TBL TpBedSelectRect = {
	//	{0,191,0,255}, ty,by,lx,rx
	 TP_BACK_Y,TP_BACK_Y+TP_BACK_H,TP_BACK_X,TP_BACK_X+TP_BACK_W
	};

	if( GF_TP_SingleHitTrg( &TpBedSelectRect )){
		return EXE_BEDSEL_BACK;
	}

	ret = touchsub_BedHitCheck(wk);

	if(ret >= 0){
		wk->c_pos = ret;
		NPSub_CursorPosSet(wk);
		return EXE_BEDSEL_DECIDE;
	}
	return -1;
}

/**
 *	@brief	ベッド選択画面　決定時処理
 */
int exesub_BedSelectDecide(NP_APPWORK* wk)
{
	SEED_STATE* sp;

	sp = &wk->seed[wk->c_pos];

	switch(sp->stat){
	case SEEDSTAT_NOTHING:
		if(sp->compost){
			return SEQ_LIST_PLANT;
		}
		return SEQ_LIST_COMPOST;
	case SEEDSTAT_UNDERGROUND:
	case SEEDSTAT_HUTABA:
	case SEEDSTAT_MIKI:
	case SEEDSTAT_FLOWER:
		return SEQ_LIST_WATER;
	case SEEDSTAT_FRUIT:
		return SEQ_LIST_HARVEST;
	case SEEDSTAT_CHANGE:
	default:
		break;
	}
	return SEQ_BED_SELECT;
}


/**
 *	@brief	戻るボタン描画
 */

